﻿using Furion.InstantMessaging;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace LNSServer.Hubs
{
    /// <summary>
    /// 聊天集线器
    /// </summary>
    [MapHub("/hubs/chathub")]
    public class ChatHub: Hub
    {
        private readonly static Dictionary<string, string> ciduser = new();
        //private readonly static Dictionary<string, string> usercid = new();
        private readonly static List<string> conIds = new();

        // 定义一个方法供客户端调用
        //public Task Login(string user)
        //{
        //    if (!usercid.ContainsKey(user)) {
        //        // 触发客户端定义监听的方法
        //        ciduser.Add(Context.ConnectionId, user);
        //        usercid.Add(user, Context.ConnectionId);
        //    }
        //    Console.WriteLine("ID对应用户 => " + JObject.FromObject(ciduser).ToString());
        //    Console.WriteLine("用户对应ID => " + JObject.FromObject(usercid).ToString());
        //    return Clients.Client(Context.ConnectionId).SendAsync("OnLoginSuccess");
        //}

        // 定义一个方法供客户端调用
        public Task Login(string user)
        {
            // 触发客户端定义监听的方法
            ciduser.Add(Context.ConnectionId, user);

            Console.WriteLine("ID对应用户 => " + JObject.FromObject(ciduser).ToString());
            return Clients.Client(Context.ConnectionId).SendAsync("OnLoginSuccess");
        }

        // 定义一个方法供客户端调用
        public Task SendMessage(string user, string message)
        {
            // 触发客户端定义监听的方法
            Console.WriteLine($"{user} : {message}");
            Console.WriteLine($"当前发送者：{Context.ConnectionId}");
            //Clients.User(Context.ConnectionId).SendAsync("ReceiveMessage", user, message);
            //JObject jo = JObject.Parse(message);
            foreach (KeyValuePair<string, string> kvp in ciduser) {
                if (kvp.Value.Equals(user)) {
                    conIds.Add(kvp.Key);
                }
            }
            return Clients.Clients(conIds).SendAsync("ReceiveMessage", user, message);
        }

        // 定义一个方法供客户端调用
        //public Task SendMessage(string user, string message)
        //{
        //    // 触发客户端定义监听的方法
        //    Console.WriteLine($"{user} : {message}");
        //    Console.WriteLine($"当前发送者：{Context.ConnectionId}");
        //    Console.WriteLine($"当前用户映射：{usercid.GetValueOrDefault(JObject.Parse(message).GetValue("user").ToString())}");
        //    //Clients.User(Context.ConnectionId).SendAsync("ReceiveMessage", user, message);
        //    //JObject jo = JObject.Parse(message);
        //    return Clients.Client(usercid[user]).SendAsync("ReceiveMessage", user, message);
        //    //return Clients.Client(usercid[user]).SendAsync("ReceiveMessage", user, message);
        //}

        public override Task OnDisconnectedAsync(Exception exception)
        {
            string cid = Context.ConnectionId;
            string user = ciduser.GetValueOrDefault(cid, string.Empty);
            ciduser.Remove(cid);
            //usercid.Remove(user);
            Console.WriteLine("连接断开。。。。。。");
            return base.OnDisconnectedAsync(exception);
        }

        public override Task OnConnectedAsync()
        {
            Console.WriteLine($"Connection Id: {Context.ConnectionId}");
            return base.OnConnectedAsync();
        }
    }
}
